import React, { Component,PropTypes} from 'react';
import {Link} from 'react-router';
import '../../Stylesheets/wx/comfirmPayMoney.css';
import {QueryPayResult,MyInfo,ProductPropertyCheck,JudgeUp} from '../../Action/auth';
import {GetQueryString} from '../../Action/rpc';

export default class PaymentSuccess extends Component {
    static contextTypes = {
        router:PropTypes.object
    }

    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            total:GetQueryString('total')?GetQueryString('total'):0,
            useAmount:0,
            stock:false,
            agent:'',
            judgeup:GetQueryString('judgeup'),
            tolink:'',
            btn:''
        };
        this.grade=GetQueryString('grade');
        this.status=false;
    }

    async componentWillMount() {
        this.props.location.query.tranType==2?this.status=true:this.status=false;

        if(this.props.location.query.tranType==2){
            await MyInfo()
                .then(res=>{
                    console.log(res.USE_AMOUNT)
                    this.setState({grade:res.AGENT_GRADE});
                    this.setState({
                        useAmount:res.USE_AMOUNT
                    })
                    sessionStorage.setItem('myAgent',res.AGENT_GRADE);
                })
                .catch(err=>{
                    console.warn('获取资料失败',err)
                });

                if(this.state.total-this.state.useAmount>0){
                    this.setState({stock:true});
                    if(this.state.judgeup!=='0'){
                        this.setState({tolink:2})
                    }else{
                        this.setState({tolink:1})
                    }
                }else{
                    this.setState({stock:false})
                    if(this.state.judgeup!=='0'){
                        this.setState({btn:'去进货'})
                        ProductPropertyCheck(this.grade)
                            .then(res=>{
                                console.log(res.USE_AMOUNT)
                            })
                            .catch(err=>{
                                console.warn('获取资料失败',err)
                            })
                    }else{
                        this.setState({btn:'返回个人中心'})
                        JudgeUp(this.grade)
                            .then(res=>{
                            })
                            .catch(err=>{
                                console.warn('获取资料失败',err)
                            })
                    }
                }
        }
    }

    continue(){
        if(this.state.tolink==1){
            this.context.router.push({pathname:'/rechargeFirst',query:{total:this.state.total,grade:this.grade,judgeup:0}})
        }else{
            this.context.router.push({pathname:'/agentGrade'})
        }
    }

    async tip(){
        if(this.state.judgeup!=='0'){
            alert('恭喜成为代理，首次进货需进最低'+(this.state.total)*0.9+'元的货物')
            sessionStorage.setItem('myAgent',this.state.agent);
            this.context.router.push({pathname:'purchase'})
        }else{
            alert('升级代理成功，返回首页')
            sessionStorage.setItem('myAgent',this.state.agent);
            this.context.router.push({pathname:'personalCenter'})
        }

    }

    render() {
        const SCREEN_WIDTH = window.screen.width
        return (
            <div className="containerNav">
                <div className="paySuccBk" style={{height:'12.5rem',paddingTop:30}}>
                    <div className="tc">
                        <img style={{height:50,width:50}} src={require("../../Images/gou.png")} alt=""/>
                    </div>
                    <div className="tc colorlv f20" style={{marginTop: 24}}>您的订单已支付成功</div>
                    <div className="f14 df flex-pack-center" style={{marginTop:20}}>
                        {
                            this.status?
                                this.state.stock?
                                    <button className="colorfff bgk_lv border_ra borderno pa_back" onClick={()=>this.continue()}>继续充值</button>
                                    :
                                    <button className="colorfff bgk_lv border_ra borderno pa_back"
                                            onClick={()=>this.tip()}
                                    >{this.state.btn}</button>
                                :
                                <Link  to="/userCenter">
                                    <button className="colorfff bgk_lv border_ra borderno pa_back">返回个人中心</button>
                                </Link>
                        }
                    </div>
                </div>
                {/*<div className="pAll">
                    <div className="flex flex-align-center">
                        <span className="color9 f20">订单详情</span>
                        <span className="di lineBack bgk_gray ml"></span>
                    </div>
                    <div style={{marginTop:15}}>
                        <span className="color9 f14 color6">订单编号:</span>
                        <span className="di ml f14 color6">545464658</span>
                    </div>
                    <div className="mt5">
                        <span className="color9 f14 color6">交易方式:</span>
                        <span className="di ml f14 color6">支付宝支付</span>
                    </div>
                    <div className="mt5">
                        <span className="color9 f14 color6">下单时间:</span>
                        <span className="di ml f14 color6">2017-02-20</span>
                    </div>
                </div>*/}

            </div>
        );
    }
}
